﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using BusinessObject;
using Utility;
using System.Web.Security;
using System.Resources;
using Ajax;
namespace WebTeamplate
{
    public partial class Admin_ChiTietPhieuNhapHang : BasePage
    {

        #region Fields
        // private string ctrAutoComplete = string.Empty;
        private string optionCurency = string.Empty;
        private int indexGenControl = 0;
        public string urlList = "DMPhieuNhapHang.aspx";
        private List<CurencyResult> lstCusrency = new List<CurencyResult>();
        List<ChiTietPhieuNhapHangResult> lstChiTiet = new List<ChiTietPhieuNhapHangResult>();

        #endregion

        #region ViewStates
        public string Command
        {

            get
            {
                object o = ViewState["Command"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["Command"] = value; }

        }
        public string userNameLogin
        {

            get
            {
                object o = ViewState["userNameLogin"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["userNameLogin"] = value; }

        }

        public Int64 MaPhieuPhap
        {

            get
            {
                object o = ViewState["MaPhieuPhap"];
                if (o != null) return (Int64)o;
                return Int64.MinValue;
            }

            set { ViewState["MaPhieuPhap"] = value; }

        }
        #endregion

        #region Init,Loads
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(WebTeamplate.Admin_ChiTietPhieuNhapHang));
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnSave.ClientID);

            if (!Page.IsPostBack)
            {


                InitParamater();
                InitTextControl();
                CommonFunction.LoadSubCate(drlKho, CommonFunction.DEFAULT_WAREHOUSE_CODE, false);
                // InitDataFormInputMH();
                InitData();
                if (this.Command == CommonFunction.DEFAULT_COMMAND_ADD)
                {
                    GenarateListFormInsert();
                    chkCommit.Visible = true;

                }
                else
                {
                    //  chkCommit.Visible = false;
                    LoadData();
                    GenarateListFormUpdate();


                }
                InitAutoCompleteData();
            }


        }
        private void InitAutoCompleteData()
        {

            string scriptATCL = @"<script>
            var customarray = new Array();            
            </script>";
            string item = string.Empty;
            for (int i = 1; i <= indexGenControl; i++)
            {
                item += "var obj" + i + " = actb(document.getElementById('txtTenMH_" + i + "'), customarray,1); ";
            }
            scriptATCL += @" <script>
           " + item + @"           
        </script>";
            ltrScript.Text = scriptATCL;

        }



        private void InitData()
        {

            //GenarateOptionLoaiHH();
            GenarateOptionDVT();
            GenarateOptionCurency();
        }

        private void LoadData()
        {
            //Load Phieu Nhap Hang Data

            PhieuNhapHang objPhieuNhapHang = PhieuNhapHangUtil.Get(this.MaPhieuPhap);
            if (objPhieuNhapHang != null)
            {
                if (objPhieuNhapHang.IsCommit)
                {
                    Response.Redirect("ViewPhieuNhapHang.aspx?id=" + objPhieuNhapHang.MaPhieuNhapHang);
                }
                chkCommit.Checked = false;
                drlKho.SelectedValue = objPhieuNhapHang.MaKho.ToString();
                txtNgayNhap.Text = ConvertValueObject.ParseString(objPhieuNhapHang.NgayNhap);
                txtNote.Text = objPhieuNhapHang.GhiChu;
                MembershipUser user = Membership.GetUser(objPhieuNhapHang.NguoiNhap);
                if (user != null)
                {
                    UserProfile profile = UserProfileUtil.Get(this.applicationName, new Guid(user.ProviderUserKey.ToString()));
                    if (profile != null)
                        txtNguoiNhap.Text = profile.Name;
                    else txtNguoiNhap.Text = objPhieuNhapHang.NguoiNhap;
                }
                else
                {
                    txtNguoiNhap.Text = objPhieuNhapHang.NguoiNhap;
                }
                //get List Data
                lstChiTiet = ChiTietPhieuNhapHangUtil.GetByMaPhieuNhapHang(this.MaPhieuPhap);
                //generate form here
                //get NhaCC
                NhaCungCap objNhaCC = NhaCungCapUtil.Get(objPhieuNhapHang.MaNhaCC);
                if (objNhaCC != null)
                {
                    txtNhaCungCap.Text = objNhaCC.TenNCC;
                }
            }

        }

        #endregion

        #region Methos

        private void InitParamater()
        {
            this.MaPhieuPhap = ConvertValueObject.ParseInt(CommonFunction.GetValueFromQueryString(this.Page, "id"));
            this.Command = CommonFunction.GetValueFromQueryString(this.Page, "act");
            if (this.Command == string.Empty)
                this.Command = CommonFunction.DEFAULT_COMMAND_ADD;
            if (this.Command == CommonFunction.DEFAULT_COMMAND_ADD)
            {
                btnCreateBarCode.Visible = false;
                btnXemBanIn.Visible = false;
                btnDelete.Visible = false;
            }
            else
            {
                btnCreateBarCode.Visible = true;
                btnXemBanIn.Visible = true;
                btnDelete.Visible = true;
            }

        }
        private void InitTextControl()
        {

            btnSave.Text = Resources.Resource.LABEL_BUTTON_SAVE;
            btnCacel.Text = Resources.Resource.LABEL_BUTTON_CANCEL;
            btnReset.Text = Resources.Resource.LABEL_BUTTON_RESET;
            //refvKho.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblKho));         
            // vsumPhieuNhapHang.ValidationGroup=refvKho.ValidationGroup=btnSave.ValidationGroup ="phieuNhapHang";
            UserProfile user = UserProfileUtil.Get(this.applicationName, this.userloginId);
            if (user != null)
                txtNguoiNhap.Text = user.Name;
            this.userNameLogin = this.userLoginName;
            txtNgayNhap.Text = hidNow.Value = ConvertValueObject.ParseString(CommonFunction.Now);
            //Init Form MH


        }
        //private void Update(SubCategory objData)
        //{
        //    // objData.SubCategoryId = this.SubCategoryID;
        //    if (!SubCategoryUtil.Update(objData))
        //        lblErrorMessage.Text = Resources.Resource.MESSAGE_UPDATE_ERROR;
        //    else
        //        Response.Redirect(urlList);

        //}
        //private void Insert(SubCategory objData)
        //{

        //    if (!SubCategoryUtil.Insert(objData))
        //        lblErrorMessage.Text = Resources.Resource.MESSAGE_INSERT_ERROR;

        //    else
        //        Response.Redirect(urlList);

        //}
        int wLoaiMH = 160;
        int wTenMH = 310;
        int wMaMH = 50;
        int wNuocSX = 80;
        int wXe = 140;
        int wMaPhuTung = 190;
        int wHieu = 120;
        int wSoLuong = 50;
        int wDVT = 70;
        int wGia = 170;
        int wTongGia = 120;
        int WDel = 40;
        int WGiaban = 40;
        int heightCtr = 26;
        private void GenarateListFormInsert()
        {
            int maxRowGenerate = 0;
            int maxRowShow = 0;
            hidTotalRow.Value = CommonFunction.PHIEUNHAP_MAXROW_GENNARATE.ToString();
            maxRowGenerate = CommonFunction.PHIEUNHAP_MAXROW_GENNARATE;
            maxRowShow = CommonFunction.PHIEUNHAP_MAXROW_SHOW;
            hidMaxRowShow.Value = maxRowShow.ToString();
            hidCurrentRowShow.Value = maxRowShow.ToString();
            //   hidTemp.Value = "<option value=''>" + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";//item for drlMatHang
            //  return;// string.Empty;

            string result = @"<table class='lvt listForm' border='0' cellpadding='3' cellspacing='1' width='100%'>                    
                    <tr>
                     
                       
                        
                        <td class='lvtCol' style='width: " + wTenMH + @"px;'>
                            Tên mặt hàng
                        </td>
                        <td class='lvtCol' style='width: " + wMaMH + @"px;'>
                            Mã MH
                        </td>
                        <td class='lvtCol' style='display:none;width: " + wMaPhuTung + @"px;'>
                            Mã phụ tùng
                        </td>
                        <td class='lvtCol' style='width: " + wXe + @"px;'>
                            Xe
                        </td>
                        <td class='lvtCol' style='width: " + wNuocSX + @"px;'>
                            Nước SX
                        </td>
                        <td class='lvtCol' style='width: " + wHieu + @"px;'>
                            Hiệu
                        </td>
                        <td class='lvtCol' style='width: " + wSoLuong + @"px;'>
                            Số lượng
                        </td>
                        <td class='lvtCol' style='width: " + wDVT + @"px;'>
                            DVT
                        </td>
                        <td class='lvtCol' style='width: " + wGia + @"px;'>
                            Giá nội bộ
                        </td>
<td class='lvtCol' style='width: " + wTongGia + @"px;'>
                            Thành tiền
                        </td>
                        <td class='lvtCol' style='width: " + WDel + @"px;'>
                        </td>
                    </tr>";
            for (int i = 1; i <= maxRowGenerate; i++)
            {
                if (i <= maxRowShow)
                    result += GenarateRowInsert(i, "");
                else result += GenarateRowInsert(i, "none");
            }
            result += @"</table>";
            tdForm.InnerHtml = result;
            indexGenControl = maxRowGenerate;

        }
        private string defaultSelectCurrency = "";
        private void GenarateListFormUpdate()
        {

            int maxRowGenerate = 0;
            int maxRowShow = 0;
            if (lstChiTiet.Count > CommonFunction.PHIEUNHAP_MAXROW_GENNARATE)
            {
                maxRowGenerate = lstChiTiet.Count;
                maxRowShow = maxRowGenerate;
            }
            else
            {
                maxRowGenerate = CommonFunction.PHIEUNHAP_MAXROW_GENNARATE;
                maxRowShow = lstChiTiet.Count;
            }
            hidTotalRow.Value = maxRowGenerate.ToString();
            hidMaxRowShow.Value = maxRowShow.ToString();
            hidCurrentRowShow.Value = maxRowShow.ToString();
            //  hidTemp.Value = "<option value=''>" + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";//item for drlMatHang

            string result = @"<table class='lvt listForm' border='0' cellpadding='3' cellspacing='1' width='100%'>                    
                    <tr>
                       
                       
                       
                        <td class='lvtCol' style='width: " + wTenMH + @"px;'>
                            Tên mặt hàng
                        </td>
                        <td class='lvtCol' style='width: " + wMaMH + @"px;'>
                            Mã MH
                        </td>
                            <td class='lvtCol' style='display:none;width: " + wMaPhuTung + @"px;'>
                            Mã phụ tùng
                        </td>
                                </td>
                            <td class='lvtCol' style='width: " + wXe + @"px;'>
                            Xe
                        </td>
                        <td class='lvtCol' style='width: " + wNuocSX + @"px;'>
                            Nước SX
                        </td>
                        <td class='lvtCol' style='width: " + wHieu + @"px;'>
                            Hiệu
                        </td>
                        <td class='lvtCol' style='width: " + wSoLuong + @"px;'>
                            Số lượng
                        </td>
                        <td class='lvtCol' style='width: " + wDVT + @"px;'>
                            DVT
                        </td>
                        <td class='lvtCol' style='width: " + wGia + @"px;'>
                            Giá nội bộ
                        </td>
<td class='lvtCol' style='width: " + wTongGia + @"px;'>
                            Thành tiền
                        </td>
                        <td class='lvtCol' style='width: " + WDel + @"px;'>
                        </td>
                    </tr>";
            //get list LOaiHH

            List<SubCategoryResult> lstLoaiHH = SubCategoryUtil.GetAllByCategory(CommonFunction.DEFAULT_LOAIHANGHOA_CODE);
            List<SubCategoryResult> lstDVT = SubCategoryUtil.GetAllByCategory(CommonFunction.DEFAULT_UNIT_CODE);
            for (int i = 1; i <= maxRowGenerate; i++)
            {
                // result += GenarateRowUpdate(i,lstLoaiHH,lstDVT);
                if (i <= maxRowShow)
                    result += GenarateRowUpdate(i, lstLoaiHH, lstDVT, "");
                else result += GenarateRowUpdate(i, lstLoaiHH, lstDVT, "none");

            }
            result += @"</table>";
            tdForm.InnerHtml = result;
            txtTongThanhTien.Value = ConvertValueObject.ParseString(sum);
            indexGenControl = maxRowGenerate;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "ResetCurency", "ResetCurency('" + defaultSelectCurrency + "'," + maxRowGenerate.ToString() + ");", true);
        }
        double sum = 0;
        private string CreateTDMaPTUpdate(string maPT, int indexRow)
        {
            if (maPT != string.Empty)
                return @"<td style='display:none;'>
                            <input type='text' value='" + maPT + @"' onblur='GetMHByMaPT(this.value," + indexRow + @")' id='txtMaPT_" + indexRow + @"' disabled='false'  style='width:98%' class='detailedViewTextBoxList ctrListForm ' />
                        </td>";
            else

                return @"<td style='display:none;'>
                            <input type='text' value='" + maPT + @"' onblur='GetMHByMaPT(this.value," + indexRow + @")' id='txtMaPT_" + indexRow + @"' disabled='true'  style='width:98%' class='detailedViewTextBoxList_Disable ctrListForm' />
                        </td>";
        }
        private string GenarateRowUpdate(int indexRow, List<SubCategoryResult> lstLoaiHangHoa, List<SubCategoryResult> lstDVT, string display)
        {

            ChiTietPhieuNhapHangResult objData = null;
            try
            {
                objData = lstChiTiet[indexRow - 1];
            }
            catch
            { }
            string result = string.Empty;
            if (objData == null)
            {
                string loaiHH = hidOptionLoaiMH.Value.Replace("selected='selected'", "");
                string option = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
                result = @"<tr style='display:" + display + "' id='tr_" + indexRow + @"' bgcolor='white' onmouseout='RowOut(this);' onmouseover='RowHover(this);'
                        class='lvtColData'>
                        
                         <td id='td_" + indexRow + @"'>
                             <input type='text' value='' id='txtTenMH_" + indexRow + @"' autocomplete='off' onblur='GetMHByname(this.value," + indexRow + @");'  style='width:" + (wTenMH - 10) + @"px;' class='detailedViewTextBoxList1 ctrListForm' />
                            
                        </td>                      
                        
                        <td>
                            <input type='text' value='' id='txtMaMH_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm txtNumber' />
                        </td>
                        <td style='display:none;'>
                            <input type='text' value='' id='txtMaPT_" + indexRow + @"' onblur='GetMHByMaPT(this.value," + indexRow + @")'  style='width:98%' class='detailedViewTextBoxList ctrListForm ' />
                        </td>
                        <td>
                            <input type='text' value='' id='txtXe_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm ' />
                        </td>

                        <td>
                             <input type='text' value='' id='txtNuocSX_" + indexRow + @"' style='width:98%' disabled='disabled' class='detailedViewTextBoxList_Disable1 ctrListForm'
                                onfocus='TextBoxLeftFocus(this);' onblur='TextBoxLeftBlur(this);' />
                        </td>
                        <td>
                            <input type='text' value='' id='txtHieu_" + indexRow + @"' style='width:98%' disabled='disabled' class='detailedViewTextBoxList_Disable1 ctrListForm'
                                onfocus='TextBoxLeftFocus(this);' onblur='TextBoxLeftBlur(this);' />
                        </td>
                        <td>
                            <input type='text' value='' id='txtSoLuong_" + indexRow + @"' style='width:98%' class='detailedViewTextBoxList ctrListForm txtNumber'
                                onfocus='TextBoxFocus(this);' onblur='TextBoxBlur(this); GetThanhTien(" + indexRow + @");' />
                        </td>
                        <td>
                            <select class='listForm ctrListForm' id='drlDonViTinh_" + indexRow + @"'  style='width:98%'>
                               " + hidOptionDVT.Value + @"
                            </select>
                        </td>


                        <td>
                            <input type='text' value='' MaxLength='14'  class='detailedViewTextBoxList1 ctrListForm txtNumber'  onfocus='TextBoxFocus(this);' onblur='TextBoxBlurGroupNumber(this); GetThanhTien(" + indexRow + @");' id='txtGiaNhap_" + indexRow + @"' style='width:60%' class=' detailedViewTextBoxList ctrListForm txtNumber'
                                  />
                            <select   id='drlCurency_" + indexRow + @"' class='ctrListForm' width='25px' onChange='ChangeCurency(this.value);'>
                               " + optionCurency + @"
                            </select>
                        </td>
                        <td>
                            <input type='text' value='' MaxLength='14' disabled='disabled' class='detailedViewTextBoxList_Disable1 ctrListForm txtNumber'   id='txtThanhTien_" + indexRow + @"' style='width:98%' 
                                  />
                        </td>

                        <td align='center'>
                            <a href='javascript:void(0);' onClick='DeleteRow(" + indexRow + @")'> Xóa</a>
                        </td>
                    </tr>";
                return result;
            }
            //get MatHang Detail
            MatHang objMatHang = MatHangUtil.Get(objData.MaMatHang);
            if (objMatHang == null)
                return string.Empty;
            //string optionLoaiMH = string.Empty;
            //optionLoaiMH = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
            //foreach (SubCategoryResult objLoaiHH in lstLoaiHangHoa)
            //{
            //    if (objMatHang.MaLoaiHangHoa == objLoaiHH.SubCategoryId)
            //        optionLoaiMH += "<option selected='selected' value='" + objLoaiHH.SubCategoryId + "'>" + objLoaiHH.SubCategoryName + "</option>";
            //    else optionLoaiMH += "<option  value='" + objLoaiHH.SubCategoryId + "'>" + objLoaiHH.SubCategoryName + "</option>";
            //}
            ////selected="selected
            //hidOptionLoaiMH.Value = optionLoaiMH;

            string optionDVT = string.Empty;
            optionDVT = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
            foreach (SubCategoryResult objDVT in lstDVT)
            {
                if (objDVT.SubCategoryId == objData.MaDonViTinh)
                    optionDVT += "<option selected='selected' value='" + objDVT.SubCategoryId + "'>" + objDVT.SubCategoryName + "</option>";
                else optionDVT += "<option  value='" + objDVT.SubCategoryId + "'>" + objDVT.SubCategoryName + "</option>";
            }
            //selected="selected
            hidOptionDVT.Value = optionDVT;


            // get List MatHang by MaMatHang
            //string optionMatHang = string.Empty;
            //List<MatHangResult> lstMatHang = MatHangUtil.GetMatHangByLoaiMatHang(objMatHang.MaLoaiHangHoa);
            //optionMatHang = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
            //foreach (MatHangResult objCate in lstMatHang)
            //{

            //    if (objCate.MaMatHang == objData.MaMatHang)
            //    {
            //        optionMatHang += "<option  selected='selected'  value='" + objCate.MaMatHang + "'>" + objCate.TenMatHang + "</option>";
            //        //get 

            //    }
            //    else optionMatHang += "<option value='" + objCate.MaMatHang + "'>" + objCate.TenMatHang + "</option>";
            //}
            double thanhTien = objData.GiaNhap * objData.SoLuongNhap;
            sum += thanhTien;
            DMXe objXe = DMXeUtil.Get(objMatHang.MaXe);
            string xe = string.Empty;
            string nuocSX = string.Empty;
            if (objXe != null)
            {

                xe = objXe.TenXe;
            }
            SubCategory objCountry = SubCategoryUtil.Get(objMatHang.MaNuocSanXuat, CommonFunction.DEFAULT_COUNTRY_CODE);
            if (objCountry != null)
                nuocSX = objCountry.SubCategoryName;
            //reser Option CurencyCode
            string curencyDrpList = string.Empty;

            foreach (CurencyResult objCurency in lstCusrency)
            {
                if (objCurency.Code.Trim() == objData.CurencyCode)
                    curencyDrpList += "<option selected='selected' value='" + objCurency.Code + "'>" + objCurency.Name + "</option>";
                else curencyDrpList += "<option value='" + objCurency.Code + "'>" + objCurency.Name + "</option>";
            }
            if (defaultSelectCurrency == "")
            {
                defaultSelectCurrency = objData.CurencyCode;
            }
            result = @"<tr style='display:" + display + "' id='tr_" + indexRow + @"' bgcolor='white' onmouseout='RowOut(this);' onmouseover='RowHover(this);'
                        class='lvtColData'>
                        
                        
                         <td id='td_" + indexRow + @"'>
                             <input type='text' value='" + objMatHang.TenMatHang + "_" + objMatHang.MaMatHang + @"' id='txtTenMH_" + indexRow + @"' autocomplete='off' onblur='GetMHByname(this.value," + indexRow + @");'  style='width:" + (wTenMH - 10) + @"px;' class='detailedViewTextBoxList1 ctrListForm' />
                            
                        </td>

                      
                        <td>
                            <input type='text' value='" + objData.MaMatHang + "' id='txtMaMH_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm txtNumber' />
                        </td>
                       " + CreateTDMaPTUpdate(objMatHang.MaPhuTung, indexRow) + @"
                        <td>
                            <input type='text' value='" + xe + @"' id='txtXe_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm ' />
                        </td>
                        
                        <td>
                             <input type='text' value='" + nuocSX + "' id='txtNuocSX_" + indexRow + @"' style='width:98%' disabled='disabled' class='detailedViewTextBoxList_Disable1 ctrListForm'
                                onfocus='TextBoxLeftFocus(this);' onblur='TextBoxLeftBlur(this);' />
                        </td>
                        <td>
                            <input type='text' value='" + objMatHang.Hieu + "' id='txtHieu_" + indexRow + @"' style='width:98%' disabled='disabled' class='detailedViewTextBoxList_Disable1 ctrListForm'
                                onfocus='TextBoxLeftFocus(this);' onblur='TextBoxLeftBlur(this);' />
                        </td>
                        <td>
                            <input type='text' value='" + ConvertValueObject.ParseString(objData.SoLuongNhap) + "' id='txtSoLuong_" + indexRow + @"' style='width:98%' class='detailedViewTextBoxList ctrListForm txtNumber'
                                onfocus='TextBoxFocus(this);' onblur='TextBoxBlur(this); GetThanhTien(" + indexRow + @");' />
                        </td>
                        <td>
                            <select class='listForm ctrListForm' id='drlDonViTinh_" + indexRow + @"'  style='width:98%'>
                               " + hidOptionDVT.Value + @"
                            </select>
                        </td>
                        <td>
                            <input type='text' value='" + ConvertValueObject.ParseString(objData.GiaNhap) + "' MaxLength='14'  class='detailedViewTextBoxList1 ctrListForm txtNumber'  onfocus='TextBoxFocus(this);' onblur='TextBoxBlurGroupNumber(this); GetThanhTien(" + indexRow + @");' id='txtGiaNhap_" + indexRow + @"' style='width:60%' class=' detailedViewTextBoxList ctrListForm txtNumber'
                                  />
                            <select   id='drlCurency_" + indexRow + @"' class='ctrListForm' width='25px' onChange='ChangeCurency(this.value);'>
                               " + curencyDrpList + @"
                            </select>
                        </td>
                        <td>
                            <input type='text' value='" + ConvertValueObject.ParseString(thanhTien) + @"' MaxLength='14' disabled='disabled' class='detailedViewTextBoxList_Disable1 ctrListForm txtNumber'   id='txtThanhTien_" + indexRow + @"' style='width:98%' 
                                  />
                        </td>
                        <td align='center'>
                            <a href='javascript:void(0);' onClick='DeleteRow(" + indexRow + @")'> Xóa</a>
                        </td>
                    </tr>";
            return result;
        }

        private string GenarateRowInsert(int indexRow, string display)
        {


            string result = @"<tr style='display:" + display + @";' id='tr_" + indexRow + @"' bgcolor='white' onmouseout='RowOut(this);' onmouseover='RowHover(this);'
                        class='lvtColData'>
                       
                        
                        
                        <td id='td_" + indexRow + @"'>
                             <input type='text' value='' id='txtTenMH_" + indexRow + @"' autocomplete='off' onblur='GetMHByname(this.value," + indexRow + @");'  style='width:" + (wTenMH - 10) + @"px;' class='detailedViewTextBoxList1 ctrListForm' />
                            
                        </td>
                        <td>
                            <input type='text' value='' id='txtMaMH_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm txtNumber' />
                        </td>
                    <td style='display:none;'>
                            <input type='text' value='' id='txtMaPT_" + indexRow + @"' onblur='GetMHByMaPT(this.value," + indexRow + @")'  style='width:98%' class='detailedViewTextBoxList ctrListForm ' />
                        </td>
<td id='tdXe_" + indexRow + @"'>
                            <input type='text' value='' id='txtXe_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm ' />
                        </td>
                        <td>
                             <input type='text' value='' id='txtNuocSX_" + indexRow + @"' style='width:98%' disabled='disabled' class='detailedViewTextBoxList_Disable1 ctrListForm'/>
                        </td>
                        <td>
                            <input type='text' value='' id='txtHieu_" + indexRow + @"' style='width:98%' disabled='disabled' class='detailedViewTextBoxList_Disable1 ctrListForm' />
                        </td>
                        <td> 
                            <input type='text' value='' id='txtSoLuong_" + indexRow + @"' style='width:98%' class='detailedViewTextBoxList ctrListForm txtNumber'
                                onfocus='TextBoxFocus(this);' onblur='TextBoxBlur(this); GetThanhTien(" + indexRow + @");' />
                        </td>
                        <td>
                            <select class='listForm ctrListForm' id='drlDonViTinh_" + indexRow + @"'  style='width:98%'>
                               " + hidOptionDVT.Value + @"
                            </select>
                        </td>
                        <td>
                            <input type='text' value='' MaxLength='12' class='detailedViewTextBoxList1 ctrListForm txtNumber'
                                onfocus='TextBoxFocus(this);' onblur='TextBoxBlurGroupNumber(this); GetThanhTien(" + indexRow + @");' id='txtGiaNhap_" + indexRow + @"' style='width:60%;' class='detailedViewTextBoxList ctrListForm txtNumber'
                                  />
<select  id='drlCurency_" + indexRow + @"' class='ctrListForm' width='25px' onChange='ChangeCurency(this.value);'>
                               " + optionCurency + @"
                            </select>
                        </td>
                        <td>
                            <input type='text' value='' MaxLength='12' class='detailedViewTextBoxList_Disable1 ctrListForm ctrListForm txtNumber'
                                 disabled='disabled' id='txtThanhTien_" + indexRow + @"' style='width:98%;' 
                                  />
                        </td>
                        <td align='center'>
                            <a href='javascript:void(0);' onClick='DeleteRow(" + indexRow + @")'> Xóa</a>
                        </td>
                    </tr>";
            return result;
        }
        private void GenarateOptionLoaiHH()
        {
            string strOptionLoaiMH = string.Empty;
            List<SubCategoryResult> lstCate = SubCategoryUtil.GetAllByCategory(CommonFunction.DEFAULT_LOAIHANGHOA_CODE);
            strOptionLoaiMH = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
            foreach (SubCategoryResult objCate in lstCate)
            {
                strOptionLoaiMH += "<option value='" + objCate.SubCategoryId + "'>" + objCate.SubCategoryName + "</option>";
            }
            hidOptionLoaiMH.Value = strOptionLoaiMH;
        }
        private void GenarateOptionCurency()
        {
            optionCurency = string.Empty;
            lstCusrency = CurencyUtil.GetAll();
            foreach (CurencyResult objData in lstCusrency)
            {
                if(objData.IsPublished)
                optionCurency += "<option value='" + objData.Code + "'>" + objData.Name + "</option>";
            }
        }
        private void GenarateOptionDVT()
        {
            string strOptionDVT = string.Empty;
            List<SubCategoryResult> lstCate = SubCategoryUtil.GetAllByCategory(CommonFunction.DEFAULT_UNIT_CODE);
            strOptionDVT = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
            foreach (SubCategoryResult objCate in lstCate)
            {
                strOptionDVT += "<option value='" + objCate.SubCategoryId + "'>" + objCate.SubCategoryName + "</option>";
            }
            hidOptionDVT.Value = strOptionDVT;

        }
        private void GenarateOptionHH()
        {
            //List<SubCategoryResult> lstCate = SubCategoryUtil.GetAllByCategory(CommonFunction.DEFAULT_LOAIHANGHOA_CODE);
            //strOptionLoaiMH = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
            //foreach (SubCategoryResult objCate in lstCate)
            //{
            //    strOptionLoaiMH += "<option value='" + objCate.SubCategoryId + "'>" + objCate.SubCategoryName + "</option>";
            //}

        }
        #endregion

        #region Events, Handler
        protected void drlLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.Command == CommonFunction.DEFAULT_COMMAND_EDIT)
                LoadData();

        }
        protected void btnCreateBarCode_Click(object sender, EventArgs e)
        {
            Response.Redirect("GenaretaBarcode.aspx?id=" + this.MaPhieuPhap);
        }
        protected void btnXemBanIn_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewPhieuNhapHang.aspx?id=" + this.MaPhieuPhap);
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            PhieuNhapHangUtil.Delete(this.MaPhieuPhap);
            Response.Redirect(urlList);
        }
        protected void btnCacel_Click(object sender, EventArgs e)
        {
            Response.Redirect(urlList);
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(this.GetCurrentPageAndParamaterUrl());
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {


        }
        #endregion

        [Ajax.AjaxMethod()]
        public string GenareteProductCode()
        {
            string result = MatHangUtil.GenerateKey().ToString();
            while (result.ToString().Length < CommonFunction.MAHH_LENGTH)
            {
                result = "0" + result.ToString();
            }
            return result.ToString();
        }
        [Ajax.AjaxMethod()]
        public string GetDVTbyLoaiMH(string loaihh)
        {
            List<SubCategoryResult> lstResult = SubCategoryUtil.GetDonViTinhByLoaiHangHoa(ConvertValueObject.ParseInt(loaihh));
            string id = string.Empty + ",";
            string text = CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + ",";
            if (lstResult != null && lstResult.Count > 0)
            {
                foreach (SubCategoryResult objDVT in lstResult)
                {
                    id += objDVT.SubCategoryId.ToString();
                    text += objDVT.SubCategoryName.ToString();
                }
            }
            return id + "*" + text;
        }
        [Ajax.AjaxMethod()]
        public string GeMatHangLoaiMH(string loaihh)
        {
            string result = string.Empty;
            List<MatHangResult> lstResult = MatHangUtil.GetMatHangByLoaiMatHang(ConvertValueObject.ParseInt(loaihh));
            result = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
            foreach (MatHangResult objCate in lstResult)
            {
                result += "<option value='" + objCate.MaMatHang + "'>" + objCate.TenMatHang + "</option>";
            }
            return result;
        }
        [Ajax.AjaxMethod()]
        public string GeMatHangInfor(string maMatHang)
        {
            MatHang objMatHang = MatHangUtil.GetMatHangByIDInPhieuNhaphang(maMatHang);
            if (objMatHang != null)
            {
                string xe = string.Empty;
                DMXe objXe = DMXeUtil.Get(objMatHang.MaXe);
                if (objXe != null)
                {
                    SubCategory objChungXe = SubCategoryUtil.Get(objXe.ChungXe, CommonFunction.DEFAULT_CHUNGLOAIXE_CODE);
                    if (objChungXe != null)
                        xe = objChungXe.SubCategoryName + " - " + objXe.TenXe;
                    else
                        xe = objXe.TenXe;
                }
                return maMatHang + "*+*" + objMatHang.MaPhuTung + "*+*" + xe + "*+*" + objMatHang.TenNuocSX + "*+*" + objMatHang.Hieu + "*+*" + objMatHang.DVT.ToString();
            }
            else return "*+*";
        }
        [Ajax.AjaxMethod()]
        public string GeMatHangInforByMaPT(string maPT)
        {
            MatHang objMatHang = MatHangUtil.GetMatHangByMaPhuTungInPhieuNhaphang(maPT);
            if (objMatHang != null)
            {
                string xe = string.Empty;
                DMXe objXe = DMXeUtil.Get(objMatHang.MaXe);
                if (objXe != null)
                    xe = objXe.TenXe;
                return objMatHang.MaMatHang + "*+*" + objMatHang.MaPhuTung + "*+*" + xe + "*+*" + objMatHang.TenNuocSX + "*+*" + objMatHang.Hieu + "*+*" + objMatHang.DVT.ToString() + "*+*" + objMatHang.TenMatHang + "_" + objMatHang.MaMatHang;
            }
            else return "*+*";
        }

        //[Ajax.AjaxMethod()]
        [Ajax.AjaxMethod(HttpSessionStateRequirement.Read)]
        public string InsertData(string command, string maphieu, string kho, string nguoinhap, string ngaynhap, string now, string data, string nhacungcap, string isCommit,string note)
        {
            DateTime now1 = CommonFunction.Now;
            //MaMH|DVT|SL|GiaNhap            
            string[] arrRow = data.Split('*');
            if (arrRow.Length > 0)
            {
                List<ChiTietPhieuNhapHang> lstChiTiet = new List<ChiTietPhieuNhapHang>();
                for (int i = 0; i < arrRow.Length - 1; i++)
                {
                    lstChiTiet.Add(GetRowData(arrRow[i].ToString()));
                }
                PhieuNhapHang objPhieuNhap = new PhieuNhapHang();
                objPhieuNhap.MaKho = ConvertValueObject.ParseInt(kho);
                objPhieuNhap.NgayNhap = CommonFunction.SetTime(ConvertValueObject.ParseDateTime(ngaynhap), now1.Hour, now1.Minute, now1.Second);

                objPhieuNhap.NguoiNhap = nguoinhap;
                objPhieuNhap.SysCreatedDate = now1;
                objPhieuNhap.SysCreatedBy = nguoinhap;
                objPhieuNhap.SysUpdatedDate = now1;
                objPhieuNhap.SysUpdatedBy = nguoinhap;
                objPhieuNhap.GhiChu =note;
                if (isCommit == "true")
                    objPhieuNhap.IsCommit = true;
                else
                    objPhieuNhap.IsCommit = false;
                NhaCungCap objNhaCC = new NhaCungCap();
                objNhaCC.TenNCC = nhacungcap;
                if (command == CommonFunction.DEFAULT_COMMAND_EDIT)
                {
                    //objPhieuNhap.IsCommit = false;
                    Int64 phieuNhapHang = ConvertValueObject.ParseLong(maphieu);
                    if (phieuNhapHang > 0)
                    {
                        objPhieuNhap.MaPhieuNhapHang = phieuNhapHang;
                        if (PhieuNhapHangUtil.Update(objPhieuNhap, lstChiTiet, objNhaCC))
                        {
                            if (objPhieuNhap.IsCommit)
                            {
                                PhieuNhapHangUtil.CommitPhieuNhapHang(objPhieuNhap.MaPhieuNhapHang, nguoinhap, CommonFunction.Now);

                            }
                            return phieuNhapHang.ToString();
                        }
                        return "0";

                    }
                    else return "0";
                }
                else
                {
                    Int64 id = PhieuNhapHangUtil.Insert(objPhieuNhap, lstChiTiet, objNhaCC);
                    if (id > 0)
                        return id.ToString();
                    return "0";
                }

            }
            return "0";
        }

        private ChiTietPhieuNhapHang GetRowData(string data)
        {
            string[] arr = data.Split(';');
            if (arr.Length > 0)
            {
                ChiTietPhieuNhapHang objData = new ChiTietPhieuNhapHang();
                objData.MaMatHang = arr[0].ToString();
                objData.SoLuongNhap = ConvertValueObject.ParseInt(arr[1].ToString());
                objData.MaDonViTinh = ConvertValueObject.ParseInt(arr[2].ToString());
                objData.GiaNhap = ConvertValueObject.ParseDouble(arr[3].ToString());
                objData.CurencyCode = arr[4].ToString();
                return objData;
            }
            return null;
        }

        #region Validate
        protected void CheckSFormMHGiaBanValid(object source, ServerValidateEventArgs args)
        {
            args.IsValid = CommonFunction.CheckSGiaIsValid(args.Value);
        }
        #endregion

    }

}